package com.gylang.print.richtext.component;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.jsoup.nodes.Element;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author gylang
 */
@Data
public abstract class BaseRich {

	private Element originNode;

	private RichBox box;

	private Map<String, Object> style = new HashMap<>();

	private List<BaseRich> child;

	private float x;

	private float y;
	/** ---------------------------------样式相关---------------------------------------------- */
	/** 大小 */
	public static final String SIZE = "SIZE";

	/** 粗体 */
	public static final String BOLD = "BOLD";
	/** 字体 */
	public static final String FONT = "FONT";

	/** 间距 */
	public static final String SPACE = "SPACE";

	/** 斜体 */
	public static final String ITALIC = "ITALIC";

	/** 下划线 */
	public static final String UNDER_LINE = "UNDER_LINE";

	/** 删除线 */
	public static final String DELETE_LINE = "DELETE_LINE";

	/** 缩进 */
	public static final String RETRACT = "RETRACT";

	/** 行高 */
	public static final String LINE_HIGH = "LINE_HIGH";

	/** 顏色 */
	public static final String COLOR = "COLOR";

	/** 背景色 */
	public static final String BACKGROUND_COLOR = "BACKGROUND_COLOR";

	/** 对齐 */
	public static final String ALIGN = "ALIGN";

	/** 代码类型 */
	public static final String CODE = "CODE";

	/** 表格边框 */
	public static final String TABLE_CODE = "TABLE_CODE";

	/** 宽度 */
	public static final String WIDTH = "WIDTH";

	/** 高度 */
	public static final String HEIGHT = "HEIGHT";

	/** 单元边沿与其内容之间的空白 */
	public static final String CELL_PADDING = "CELL_PADDING";

	/** 是单元之间的空间 */
	public static final String CELL_SPACING = "CELL_SPACING";

	public static final String FONT_SIZE = "FONT_SIZE";


	/** 继承父类样式 */
	public void copyStyle(BaseRich baseRich) {
		if (null != baseRich) {
			this.style = new HashMap<>(baseRich.getStyle());
		}
	}

	public void setIfNoNull(String styleKey, Object obj) {
		if (null != obj) {
			style.put(styleKey, obj);
		}
	}

	public void setIfNoEmpty(String styleKey, String obj) {
		if (StrUtil.isNotEmpty(obj)) {
			style.put(styleKey, obj);
		}
	}

	public void setIfNoEmpty(String styleKey, Iterable<?> obj) {
		if (CollUtil.isNotEmpty(obj)) {
			style.put(styleKey, obj);
		}
	}

	public void setIfNoEmpty(String styleKey, Map<?, ?> obj) {
		if (CollUtil.isNotEmpty(obj)) {
			style.put(styleKey, obj);
		}
	}

	public <T> T getNullDefault(String styleKey, T t) {
		Object o = style.get(styleKey);
		if (null != o) {
			return (T) o;
		}
		return t;

	}

	public <T> T get(String styleKey) {

		return (T) style.get(styleKey);

	}

	public <T> void isPrecent(String styleKey, Consumer<T> consumer) {

		Object o = style.get(styleKey);
		if (null != o) {
			consumer.accept((T) o);
		}

	}


	public enum Align {
		/** 左对齐 */
		LEFT,
		/** 右对齐 */
		RIGHT,
		/** 居中 */
		CENTER,
		/** 两端对齐 */
		JUSTIFY;

		public static Align getByName(String name) {
			if (StrUtil.isEmpty(name) || LEFT.name().equalsIgnoreCase(name)) {
				return LEFT;
			} else if (RIGHT.name().equalsIgnoreCase(name)) {
				return RIGHT;
			} else if (CENTER.name().equalsIgnoreCase(name)) {
				return CENTER;
			} else {
				return JUSTIFY;
			}
		}
	}

	/**
	 * 宽度
	 *
	 * @return
	 */
	public abstract float width();

	/**
	 * 高度
	 *
	 * @return
	 */
	public abstract float height();

	/**
	 * 不满足整体换行
	 * @return
	 */
	public abstract boolean notFillBr();
}
